0a06b08e4649ec728a423a70caf425ba92935247,framework/base/src/base/org/ofbiz/base/util/ObjectType.java,ObjectType,simpleTypeConvert,#Object#String#String#TimeZone#Locale#boolean#,445

Before Change


            } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                    Number tempNum = nf.parse(str);

                    return Double.valueOf(tempNum.doubleValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                    Number tempNum = nf.parse(str);

                    return Float.valueOf(tempNum.floatValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                    nf.setMaximumFractionDigits(0);
                    Number tempNum = nf.parse(str);

                    return Long.valueOf(tempNum.longValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                    nf.setMaximumFractionDigits(0);
                    Number tempNum = nf.parse(str);

                    return Integer.valueOf(tempNum.intValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
                DateFormat df = null;
                if (format == null || format.length() == 0) {
                    df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, null);
                } else {
                    df = UtilDateTime.toDateFormat(format, timeZone, null);
                }
                try {
                    Date fieldDate = df.parse(str);
                    return new java.sql.Date(fieldDate.getTime());
                } catch (ParseException e1) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1);
                }
            } else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
                DateFormat df = null;
                if (format == null || format.length() == 0) {
                    df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, null);
                } else {
                    df = UtilDateTime.toTimeFormat(format, timeZone, null);
                }
                try {
                    Date fieldDate = df.parse(str);
                    return new java.sql.Time(fieldDate.getTime());
                } catch (ParseException e1) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1);
                }
            } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
                DateFormat df = null;
                if (format == null || format.length() == 0) {
                    df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, null);
                    // if time is missing add zeros
                    if (str.length() > 0 && !str.contains(":")) {
                    	str = str + " 00:00:00.00";
                    }
                    // hack to mimic Timestamp.valueOf() method
                    if (str.length() > 0 && !str.contains(".")) {
                        str = str + ".0";
                    } else {
                        // DateFormat has a funny way of parsing milliseconds:
                        // 00:00:00.2 parses to 00:00:00.002
                        // so we'll add zeros to the end to get 00:00:00.200
                        String[] timeSplit = str.split("[.]");
                        if (timeSplit.length > 1 && timeSplit[1].length() < 3) {
                            str = str + "000".substring(timeSplit[1].length());
                        }
                    }
                } else {
                    df = UtilDateTime.toDateTimeFormat(format, timeZone, null);
                }
                try {
                    Date fieldDate = df.parse(str);
                    return new java.sql.Timestamp(fieldDate.getTime());
                } catch (ParseException e1) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1);
                }
            } else if ("List".equals(type) || "java.util.List".equals(type)) {
                if (str.startsWith("[") && str.endsWith("]")) {
                    return StringUtil.toList(str);
                } else {
                    List<String> tempList = FastList.newInstance();
                    tempList.add(str);
                    return tempList;
                }
            } else if ("Set".equals(type) || "java.util.Set".equals(type)) {
                if (str.startsWith("[") && str.endsWith("]")) {
                    return StringUtil.toSet(str);
                } else {
                    Set<String> tempSet = FastSet.newInstance();
                    tempSet.add(str);
                    return tempSet;
                }
            } else if (("Map".equals(type) || "java.util.Map".equals(type)) &&
                    (str.startsWith("{") && str.endsWith("}"))) {
                return StringUtil.toMap(str);
            } else {
                throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
            }
        } else if (obj instanceof Double) {
            fromType = "Double";
            Double dbl = (Double) obj;

            if ("String".equals(type) || "java.lang.String".equals(type)) {
                NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                return nf.format(dbl.doubleValue());
            } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
                return new BigDecimal(dbl.doubleValue());
            } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
                return obj;
            } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
                return Float.valueOf(dbl.floatValue());
            } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
                return Long.valueOf(Math.round(dbl.doubleValue()));
            } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
                return Integer.valueOf((int) Math.round(dbl.doubleValue()));
            } else if ("List".equals(type) || "java.util.List".equals(type)) {
                List<Double> tempList = FastList.newInstance();
                tempList.add(dbl);
                return tempList;
            } else if ("Set".equals(type) || "java.util.Set".equals(type)) {
                Set<Double> tempSet = FastSet.newInstance();
                tempSet.add(dbl);
                return tempSet;
            } else {
                throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
            }
        } else if (obj instanceof Float) {
            fromType = "Float";
            Float flt = (Float) obj;

            if ("String".equals(type)) {
                NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                return nf.format(flt.doubleValue());
            } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
                return new BigDecimal(flt.doubleValue());
            } else if ("Double".equals(type)) {
                return Double.valueOf(flt.doubleValue());
            } else if ("Float".equals(type)) {
                return obj;
            } else if ("Long".equals(type)) {
                return Long.valueOf(Math.round(flt.doubleValue()));
            } else if ("Integer".equals(type)) {
                return Integer.valueOf((int) Math.round(flt.doubleValue()));
            } else if ("List".equals(type) || "java.util.List".equals(type)) {
                List<Float> tempList = FastList.newInstance();
                tempList.add(flt);
                return tempList;
            } else if ("Set".equals(type) || "java.util.Set".equals(type)) {
                Set<Float> tempSet = FastSet.newInstance();
                tempSet.add(flt);
                return tempSet;
            } else {
                throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
            }
        } else if (obj instanceof Long) {
            fromType = "Long";
            Long lng = (Long) obj;

            if ("String".equals(type) || "java.lang.String".equals(type)) {
                NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                return nf.format(lng.longValue());
            } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
                return BigDecimal.valueOf(lng.longValue());
            } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
                return Double.valueOf(lng.doubleValue());
            } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
                return Float.valueOf(lng.floatValue());
            } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
                return obj;
            } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
                return Integer.valueOf(lng.intValue());
            } else if ("List".equals(type) || "java.util.List".equals(type)) {
                List<Long> tempList = FastList.newInstance();
                tempList.add(lng);
                return tempList;
            } else if ("Set".equals(type) || "java.util.Set".equals(type)) {
                Set<Long> tempSet = FastSet.newInstance();
                tempSet.add(lng);
                return tempSet;
            } else {
                throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
            }
        } else if (obj instanceof Integer) {
            fromType = "Integer";
            Integer intgr = (Integer) obj;
            if ("String".equals(type) || "java.lang.String".equals(type)) {
                NumberFormat nf = locale == null ? NumberFormat.getNumberInstance() : NumberFormat.getNumberInstance(locale);
                return nf.format(intgr.longValue());
            } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
                return BigDecimal.valueOf(intgr.longValue());

After Change


     * @return
     * @throws GeneralException
     */
    public static Object simpleTypeConvert(Object obj, String type, String format, TimeZone timeZone, Locale locale, boolean noTypeFail) throws GeneralException {
        if (obj == null) {
            return null;
        }

        if (obj.getClass().getName().equals(type)) {
            return obj;
        }
        if ("PlainString".equals(type)) {
            return obj.toString();
        }
        if ("Object".equals(type) || "java.lang.Object".equals(type)) {
            return obj;
        }

        if (timeZone == null) {
            timeZone = TimeZone.getDefault();
        }

        if (locale == null) {
            locale = Locale.getDefault();
        }

        String fromType = null;

        if ((type.equals("List") || type.equals("java.util.List")) && obj.getClass().isArray()) {
            List<Object> newObj = FastList.newInstance();
            int len = Array.getLength(obj);
            for (int i = 0; i < len; i++) {
                newObj.add(Array.get(obj, i));
            }
            return newObj;        
        } else if (obj instanceof java.lang.String) {
            fromType = "String";
            String str = (String) obj;
            if ("String".equals(type) || "java.lang.String".equals(type)) {
                return obj;
            }
            if (str.length() == 0) {
                return null;
            }
            
            if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) {
                str = StringUtil.removeSpaces(str);
                return str.equalsIgnoreCase("TRUE") ? Boolean.TRUE : Boolean.FALSE;
            } else if ("Locale".equals(type) || "java.util.Locale".equals(type)) {
                Locale loc = UtilMisc.parseLocale(str);
                if (loc != null) {
                    return loc;
                } else {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ");    
                }
            } else if ("TimeZone".equals(type) || "java.util.TimeZone".equals(type)) {
                TimeZone tz = UtilDateTime.toTimeZone(str);
                if (tz != null) {
                    return tz;
                } else {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ");    
                }
            } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = NumberFormat.getNumberInstance(locale);
                    Number tempNum = nf.parse(str);
                    return new BigDecimal(tempNum.toString());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = NumberFormat.getNumberInstance(locale);
                    Number tempNum = nf.parse(str);

                    return Double.valueOf(tempNum.doubleValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = NumberFormat.getNumberInstance(locale);
                    Number tempNum = nf.parse(str);

                    return Float.valueOf(tempNum.floatValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = NumberFormat.getNumberInstance(locale);
                    nf.setMaximumFractionDigits(0);
                    Number tempNum = nf.parse(str);

                    return Long.valueOf(tempNum.longValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
                str = StringUtil.removeSpaces(str);
                try {
                    NumberFormat nf = NumberFormat.getNumberInstance(locale);
                    nf.setMaximumFractionDigits(0);
                    Number tempNum = nf.parse(str);

                    return Integer.valueOf(tempNum.intValue());
                } catch (ParseException e) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
                }
            } else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
                DateFormat df = null;
                if (format == null || format.length() == 0) {
                    df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, locale);
                } else {
                    df = UtilDateTime.toDateFormat(format, timeZone, locale);
                }
                try {
                    Date fieldDate = df.parse(str);
                    return new java.sql.Date(fieldDate.getTime());
                } catch (ParseException e1) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1);
                }
            } else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
                DateFormat df = null;
                if (format == null || format.length() == 0) {
                    df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, locale);
                } else {
                    df = UtilDateTime.toTimeFormat(format, timeZone, locale);
                }
                try {
                    Date fieldDate = df.parse(str);
                    return new java.sql.Time(fieldDate.getTime());
                } catch (ParseException e1) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1);
                }
            } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
                DateFormat df = null;
                if (format == null || format.length() == 0) {
                    df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
                    // if time is missing add zeros
                    if (str.length() > 0 && !str.contains(":")) {
                    	str = str + " 00:00:00.00";
                    }
                    // hack to mimic Timestamp.valueOf() method
                    if (str.length() > 0 && !str.contains(".")) {
                        str = str + ".0";
                    } else {
                        // DateFormat has a funny way of parsing milliseconds:
                        // 00:00:00.2 parses to 00:00:00.002
                        // so we'll add zeros to the end to get 00:00:00.200
                        String[] timeSplit = str.split("[.]");
                        if (timeSplit.length > 1 && timeSplit[1].length() < 3) {
                            str = str + "000".substring(timeSplit[1].length());
                        }
                    }
                } else {
                    df = UtilDateTime.toDateTimeFormat(format, timeZone, locale);
                }
                try {
                    Date fieldDate = df.parse(str);
                    return new java.sql.Timestamp(fieldDate.getTime());
                } catch (ParseException e1) {
                    throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1);
                }
            } else if ("List".equals(type) || "java.util.List".equals(type)) {
                if (str.startsWith("[") && str.endsWith("]")) {
                    return StringUtil.toList(str);
                } else {
                    List<String> tempList = FastList.newInstance();
                    tempList.add(str);
                    return tempList;
                }
            } else if ("Set".equals(type) || "java.util.Set".equals(type)) {
                if (str.startsWith("[") && str.endsWith("]")) {
                    return StringUtil.toSet(str);
                } else {
                    Set<String> tempSet = FastSet.newInstance();
                    tempSet.add(str);
                    return tempSet;
                }
            } else if (("Map".equals(type) || "java.util.Map".equals(type)) &&
                    (str.startsWith("{") && str.endsWith("}"))) {
                return StringUtil.toMap(str);
            } else {
                throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
            }
        } else if (obj instanceof Double) {
            fromType = "Double";
            Double dbl = (Double) obj;

            if ("String".equals(type) || "java.lang.String".equals(type)) {
                NumberFormat nf = NumberFormat.getNumberInstance(locale);
                return nf.format(dbl.doubleValue());
            } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
                return new BigDecimal(dbl.doubleValue());
            } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
                return obj;
            } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
                return Float.valueOf(dbl.floatValue());
            } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
                return Long.valueOf(Math.round(dbl.doubleValue()));
            } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
                return Integer.valueOf((int) Math.round(dbl.doubleValue()));
            } else if ("List".equals(type) || "java.util.List".equals(type)) {
                List<Double> tempList = FastList.newInstance();
                tempList.add(dbl);
                return tempList;
            } else if ("Set".equals(type) || "java.util.Set".equals(type)) {
                Set<Double> tempSet = FastSet.newInstance();
                tempSet.add(dbl);
                return tempSet;
            } else {
                throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
            }
        } else if (obj instanceof Float) {
            fromType = "Float";
            Float flt = (Float) obj;

            if ("String".equals(type)) {
                NumberFormat nf = NumberFormat.getNumberInstance(locale);
                return nf.format(flt.doubleValue());
            } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
                return new BigDecimal(flt.doubleValue());